using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameplayNotificationController : MonoBehaviour
{
	[Serializable]
	public struct GPNotificationMessage
	{
		public string text;

		public int priority;

		public Color color;
	}

	public static GameplayNotificationController instance;

	public UILabel lable;

	public List<GPNotificationMessage> messagesQueue = new List<GPNotificationMessage>();

	private Color _defaultColor;

	private void Awake()
	{
		instance = this;
		if (lable != null)
		{
			lable.gameObject.SetActive(false);
			_defaultColor = lable.color;
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Update()
	{
		if (lable != null && !lable.gameObject.activeSelf && messagesQueue.Count > 0)
		{
			ShowNotification(messagesQueue[0].text, messagesQueue[0].color);
			messagesQueue.RemoveAt(0);
		}
		if (!settings.offlineMode && !PVPManager.instance.gameIsStarted)
		{
			ClearBufer();
		}
	}

	public void ClearBufer()
	{
		messagesQueue.Clear();
		lable.gameObject.SetActive(false);
	}

	public void AddNotification(string text, int priority, bool allowDuplicate = true)
	{
		if (allowDuplicate || ((!(lable != null) || !lable.gameObject.activeInHierarchy || !(lable.text == text)) && !messagesQueue.Any((GPNotificationMessage message) => message.text == text)))
		{
			AddNotificationInternal(text, priority, _defaultColor);
		}
	}

	public void AddNotification(string text, int priority, Color color)
	{
		AddNotificationInternal(text, priority, color);
	}

	private void AddNotificationInternal(string text, int priority, Color color)
	{
		GPNotificationMessage item = default(GPNotificationMessage);
		item.priority = priority;
		item.text = text;
		item.color = color;
		messagesQueue.Add(item);
		messagesQueue.Sort((GPNotificationMessage x, GPNotificationMessage y) => x.priority.CompareTo(y.priority));
	}

	private void ShowNotification(string text, Color color)
	{
		if (lable != null && GameInfoWindow.instance != null && !GameInfoWindow.instance.holder.activeSelf && !MessageWindow.instance.panelLoading.activeSelf)
		{
			lable.text = text;
			lable.color = color;
			lable.gameObject.SetActive(true);
		}
	}
}
